//
//  ListTrainCell.swift
//  AirTravel
//
//  Created by apple on 2021/8/10.
//

import UIKit

class ListTrainCell: UITableViewCell {
    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var timeLab: UILabel!
    @IBOutlet weak var statTimeLab: UILabel!
    @IBOutlet weak var statLab: UILabel!
    
    @IBOutlet weak var priceLab: UILabel!
    @IBOutlet weak var endLab: UILabel!
    @IBOutlet weak var endTimeLab: UILabel!
    /// 车次
    @IBOutlet weak var trainsLab: UILabel!
    
    /// 硬座
    @IBOutlet weak var hardSeatLab: UILabel!
    /// 硬卧
    @IBOutlet weak var hardSleeperLab: UILabel!
    /// 软卧
    @IBOutlet weak var softSleeperLab: UILabel!
    /// 无座
    @IBOutlet weak var noSeatLab: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.backView.layer.cornerRadius = 6
        self.backView.layer.masksToBounds = true
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setUpData(dic:Dictionary<String,Any>) {
        self.statTimeLab.text = "\(dic["startTime"] ?? "")"
        self.endTimeLab.text = "\(dic["arriveTime"] ?? "")"
        let durationMinutes = "\(dic["durationMinutes"] ?? "0")"
        let dur = Int(durationMinutes)
        let sfm = self.secondsToHoursMinutesSeconds(seconds: dur ?? 0)
        self.timeLab.text = sfm
        self.statLab.text = "\(dic["fromStationName"] ?? "")"
        self.endLab.text = "\(dic["toStationName"] ?? "")"
        self.trainsLab.text = "\(dic["trainNo"] ?? "")"
        self.priceLab.text = "￥\(dic["sortPrice"] ?? "")"
        let zuos = [self.hardSeatLab,self.hardSleeperLab,self.softSleeperLab,self.noSeatLab]
        let seatList = dic["seatList"] as? Array<Any>
        if let array = seatList {
            for item in zuos {
                item?.isHidden = true
            }
            for (index,item) in array.enumerated() {
                let btn = zuos[index]
                let dic = item as? Dictionary<String,Any>
                btn?.text = "\(dic?["seatName"] ?? "")" + "\(dic?["ticketLeft"] ?? "") 张"
                let ticketLeft = Int("\(dic?["ticketLeft"] ?? "")") ?? 0
                if ticketLeft > 0 {
                    btn?.isHidden = false
                }
            }
        }
        
        
    }
    
    func secondsToHoursMinutesSeconds(seconds:Int) -> (String) {
        let h = seconds/60
        let m = seconds%60
        if h > 0 {
            if m > 0 {
                return "\(h)时\(m)分"
            }
            return "\(h)时"
        }else{
            if m > 0 {
                return "\(m)分"
            }
            return ""
        }
    }
    
}
